package com.leizi.igym.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leizi.igym.constant.TrainerConstant;
import com.leizi.igym.dto.TrainerPageQueryDTO;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import com.leizi.igym.resp.PageResult;
import com.leizi.igym.resp.Result;
import com.leizi.igym.service.TrainerService;
import com.leizi.igym.vo.TrainerVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-25  21:36
 * @Description:
 */

@RestController("userTrainerController")
@RequestMapping("/user/trainer")
@Tag(name = "用户端-教练相关接口")
@Slf4j
public class TrainerController {

    @Resource
    private TrainerService trainerService;

    @Operation(summary = "用户端：获取教练信息")
    @GetMapping
    public Result get(@RequestParam(value = "type", defaultValue = TrainerConstant.ALL_TRAINER) String type) {
        if (StringUtils.isBlank(type)){
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR,"请求教练信息异常！");
        }
        if (!Arrays.asList(TrainerConstant.ALL_TRAINER,TrainerConstant.HOME_TRAINERS).contains(type)){
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR,"请求参数异常！");
        }
        if (type.equals(TrainerConstant.ALL_TRAINER)){
            return Result.ok(trainerService.getAll());
        }
        Page<TrainerVO> trainerPage = trainerService.getTrainerPage(TrainerPageQueryDTO.builder()
                .pageSize(4)
                .current(1)
                .build());
        return Result.ok(trainerPage.getRecords());
    }

    @Operation(summary = "用户端：根据id获取教练信息")
    @GetMapping("{id}")
    public Result getById(@PathVariable("id")Long id){
        TrainerVO trainerVO = trainerService.getByIdWithExpertise(id);
        return Result.ok(trainerVO);
    }
}
